package com.yangguang.huahui.ui.fragments;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.yangguang.huahui.R;
import com.yangguang.huahui.baseadapter.BaseQuickAdapter;
import com.yangguang.huahui.baseadapter.BaseViewHolder;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.entity.PublishList;
import com.yangguang.huahui.mvp.presenter.PublishListPresent;
import com.yangguang.huahui.mvp.view.PublishListView;
import com.yangguang.huahui.ui.activitys.PublishActivity;
import com.yangguang.huahui.utils.CommonUtil;
import com.yangguang.huahui.utils.DialogUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/3/29.
 */

public class PublishListFragment extends BaseMvpFragment<PublishListView, PublishListPresent> implements PublishListView {
    public static PublishListFragment newInstance(int type) {
        PublishListFragment fragment = new PublishListFragment();
        Bundle arguments = new Bundle();
        arguments.putInt("type", type);
        fragment.setArguments(arguments);
        return fragment;
    }

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    PublishListAdapter mAdapter;
    LinearLayoutManager layoutManager;
    List<PublishList.ListBean> datas = new ArrayList<>();

    @Override
    protected int initLayoutId() {
        return R.layout.frag_message;
    }

    @Override
    protected void initView() {
        layoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new PublishListAdapter(R.layout.publish_list_item, datas);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onSuccess(PublishList result) {
        if (result.getList() != null) {
            datas = result.getList();
        }
        mAdapter.setNewData(datas);
    }

    @Override
    public void onError() {

    }

    @Override
    protected PublishListPresent initPresenter() {
        return new PublishListPresent(getArguments().getInt("type"));
    }

    @Override
    protected void fetchData() {
        mPresenter.getPublish(DataInfo.TOKEN);
    }

    private class PublishListAdapter extends BaseQuickAdapter<PublishList.ListBean> {
        public PublishListAdapter(int layoutResId, List<PublishList.ListBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final PublishList.ListBean item) {
            helper.setText(R.id.tv_content, item.getContent());
            helper.setOnClickListener(R.id.tv_delete, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DialogUtil.showOKCancelDialog(mContext, "", getString(R.string.publish_delet_message), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            switch (i) {
                                case DialogInterface.BUTTON_POSITIVE:
                                    mPresenter.delete(item.getId());
                                    mData.remove(item);
                                    notifyDataSetChanged();
                                    break;
                                case DialogInterface.BUTTON_NEGATIVE:
                                    break;
                            }
                            dialogInterface.dismiss();
                        }
                    });
                }
            });
            helper.setOnClickListener(R.id.tv_refresh, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mPresenter.refresh(item.getId(), DataInfo.TOKEN);
                }
            });
            helper.setOnClickListener(R.id.tv_edit, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(mContext, PublishActivity.class)
                            .putExtra("type", 2)
                            .putExtra("position", getArguments().getInt("type") - 1)
                            .putExtra("id", item.getId()));
                }
            });
            if (CommonUtil.isNull(item.getRelease_time())) return;
            helper.setText(R.id.tv_date, CommonUtil.longToTime(Long.parseLong(item.getRelease_time() + "000"), Calendar.DATE));
        }
    }
}
